package com.panshi.controller;

import com.panshi.entity.AppEssayT;
import com.panshi.entity.AppMatterT;
import com.panshi.entity.ClassifyT;
import com.panshi.service.AnswerClientService;
import com.panshi.service.QaClientService;
import com.panshi.service.UserClientService;
import com.panshi.service.ZxClientService;
import com.panshi.util.UserInfoUtils;
import com.panshi.util.ZxUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;

@Controller
public class IndexController {

    @Autowired
    private UserInfoUtils userInfoUtils;

    @Autowired
    private ZxUtils zxUtils;

    @Autowired
    private ZxClientService zxClientService;

    @Autowired
    private QaClientService qaClientService;

    //回答ID
    @Autowired
    private AnswerClientService answerClientService;

    @Autowired
    private UserClientService userClientService;

    @RequestMapping(value = {"/main","/"})
    public String index(HttpServletRequest request, Model model, HttpServletResponse response){
        System.out.println("初始化数据---------》");
        List<ClassifyT> zxClassifyTList = zxClientService.call();
        //咨询分类列表
        model.addAttribute("zxClassifyTList",zxClassifyTList);

        Object queryLimit = model.getAttribute("queryLimit");

        //问答列表
        List<AppMatterT> qaAppMatterTList = qaClientService.amall();

        if(qaAppMatterTList!=null&&!qaAppMatterTList.isEmpty()){
            qaAppMatterTList.forEach(matterT->{
                //获取问答分类
                matterT.setClassifyT(qaClientService.bycid(matterT.getClassifyId()));
                //获取答题对象
                matterT.setAnswerT(answerClientService.bymid(matterT.getMatterId()));
                //获取用户对象
                matterT.setAppUserT(userClientService.byid(matterT.getUserId()));
                //回答用户对象
                if(matterT.getAnswerT()!=null){
                    matterT.setReplyUser(userClientService.byid(matterT.getAnswerT().getUserId()));
                }
            });
            Collections.shuffle(qaAppMatterTList);
            model.addAttribute("qaAppMatterTList",qaAppMatterTList);

        }

        //初始化咨询
        List<AppEssayT> appEssayTS = zxUtils.zxList(1, 5, "-1");
        Collections.shuffle(appEssayTS);
        model.addAttribute("zxAppEssayTList",appEssayTS);

        //热读排行
        List<AppEssayT> appEssayTS1 = zxUtils.zxList(1, 5, "read_count");
        Collections.shuffle(appEssayTS1);
        model.addAttribute("readEssayTS",appEssayTS1);

        userInfoUtils.veruser(request,model,response);

        return "main";
    }






}
